/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.QuestionCategoryDAO;
import model.dto.QuestionCategory;
import model.util.SharingUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author PhucAn
 */
public class QuestionCategoryAction extends BaseAction {

    QuestionCategoryDAO daoQCate = new QuestionCategoryDAO();

    public ActionForward manage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ArrayList<QuestionCategory> list = daoQCate.getAllCategories();
        request.setAttribute("list", list);
        request.setAttribute(CONTENT_HOLDER, "qcate-manage");
        return mapping.findForward("home");
    }

//    public ActionForward add(ActionMapping mapping, ActionForm form,
//            HttpServletRequest request, HttpServletResponse response)
//            throws Exception {
//
//        request.setAttribute(BaseAction.CONTENT_HOLDER, "qcate-add");
//        return mapping.findForward("home");
//    }
    public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        QuestionCategory qcate = new QuestionCategory();
        qcate.setName(request.getParameter("name") == null ? "" : request.getParameter("name"));
        qcate.setDescription(request.getParameter("description") == null ? "" : request.getParameter("description"));
        if (daoQCate.create(qcate) > 0) {
            SharingUtil.showMessage(request, new ActionMessage(MSG_CREATE_SUCCESS));
        } else {
            SharingUtil.showMessage(request, new ActionMessage(MSG_CREATE_FAILURE));
        }
        request.setAttribute(BaseAction.CONTENT_HOLDER, "qcate-manage");
        return mapping.findForward("home");
    }
}
